package rhinestone.Model;

import java.util.HashMap;
import java.io.Serializable;

/**
 * The container of all available packs.
 *
 * @author Group 2
 * @version 2010.12.16
 */
public class PackContainer implements Serializable {
	private HashMap<String, Pack> packs;
	private static PackContainer instance;
	private boolean changed;

	/**
	 * Private constructor for instance variable initialization.
	 */
	private PackContainer() {
		packs = new HashMap<String, Pack>();
		changed = false;
	}

	/**
	 * Singleton pattern.
	 * @return The object of this container.
	 */
	public static PackContainer getInstance() {
		if(instance == null)
			instance = new PackContainer();
		return instance;
	}

	/**
	 * Adds pack to the database.
	 * @param pack The pack to be added.
	 */
	public void addPack(Pack pack) {
		packs.put(pack.getBarcode(), pack);
		changed = true;
	}

	/**
	 * Removes the pack with the provided bar code from the database.
	 * @param barcode The bar code of the pack to remove.
	 */
	public Pack removePack(String barcode) {
		Pack pack = packs.remove(barcode);
		if(pack != null) {
			System.out.println(" --- Pack removed ---");
			changed = true;
		}
		return pack;
	}

	/**
	 * Looks for the pack with the provided bar code in the database.
	 * @param barcode The bar code of the product.
	 * @return The Pack object (null if not found).
	 */
	public Pack getPack(String barcode) {
		return packs.get(barcode);
	}

	public boolean isChanged() {
		return changed;
	}

	public void setChanged(boolean changed) {
		this.changed = changed;
	}
}
